package com.suypower.carManage.action;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.comm.ConstUtil;

import javax.servlet.ServletException;
import java.util.ArrayList;

public class DeleteAction extends ActionSupport {

    private String type;
    private String id;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String execute() {
        String res = "";
        if (type.equals("driver")) {
            res = this.DeleteDriver();
        } else if (type.equals("refuel")) {
            res = this.DeleteRefuel();
        } else if (type.equals("repair")) {
            res = this.deleteRows("tb_repair");
        } else if (type.equals("car")) {
            res = this.deleteRows("tb_car");
        } else if (type.equals("peccancy")) {
            res = this.deleteRows("tb_peccancy");
        } else if (type.equals("refuelCard")) {
            res = this.deleteRows("tb_refuel_card");
        } else if (type.equals("recharge")) {
            res = this.deleteRows("tb_refuel_card_recharge");
        } else if (type.equals("secure")) {
            res = this.deleteRows("tb_secure");
        } else if (type.equals("fee")) {
            res = this.deleteRows("tb_fee");
        } else if (type.equals("milage")) {
            res = this.deleteRows("tb_milage");
        } else if (type.equals("maintain")) {
            res = this.deleteRows("tb_maintain");
        } else if (type.equals("metadata")) {
            res = this.deleteRows("tb_metadata");
        } else if (type.equals("user")) {
            res = this.deleteRows("tb_user", "userid");
        } else if (type.equals("role")) {
            res = this.deleteRows("tb_role");
        } else if (type.equals("tyre")) {
            res = this.deleteRows("tb_tyre");
        } else if (type.equals("tolls")) {
            res = this.deleteRows("tb_tolls");
        }
        return res;
    }

    /**
     * 删除驾驶员
     *
     * @return
     */
    private String DeleteDriver() {
        DAO dao = new DAO();
        if (id != null && !id.equals("")) {
            String[] id_s = id.split(",");
            ArrayList<String> sqls = new ArrayList<String>();
            for (String str : id_s) {
                sqls.add("DELETE FROM tb_driver WHERE id=" + str);
            }
            try {
                boolean b = dao.execCommand(ConstUtil.DBNAME, sqls);
                if (b == true) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } catch (ServletException e) {
                return this.ERROR;
            }
        } else {
            return this.ERROR;
        }
    }

    /**
     * 删除加油记录
     *
     * @return
     */
    public String DeleteRefuel() {
        DAO dao = new DAO();
        if (id != null && !id.equals("")) {
            String[] id_s = id.split(",");
            ArrayList<String> sqls = new ArrayList<String>();
            for (String str : id_s) {
                sqls.add("DELETE FROM tb_refuel WHERE id=" + str);
            }
            try {
                boolean b = dao.execCommand(ConstUtil.DBNAME, sqls);
                if (b == true) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } catch (ServletException e) {
                return this.ERROR;
            }
        } else {
            return this.ERROR;
        }
    }

    public String deleteRows(String table) {
        DAO dao = new DAO();
        if (id != null && !id.equals("")) {
            String[] id_s = id.split(",");
            ArrayList<String> sqls = new ArrayList<String>();
            for (String str : id_s) {
                sqls.add("DELETE FROM " + table + " WHERE id=" + str);
            }
            try {
                boolean b = dao.execCommand(ConstUtil.DBNAME, sqls);
                if (b == true) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } catch (ServletException e) {
                return this.ERROR;
            }
        } else {
            return this.ERROR;
        }
    }

    public String deleteRows(String table, String rowName) {
        DAO dao = new DAO();
        if (id != null && !id.equals("")) {
            String[] id_s = id.split(",");
            ArrayList<String> sqls = new ArrayList<String>();
            for (String str : id_s) {
                sqls.add("DELETE FROM " + table + " WHERE " + rowName + "=" + str);
            }
            try {
                boolean b = dao.execCommand(ConstUtil.DBNAME, sqls);
                if (b == true) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } catch (ServletException e) {
                return this.ERROR;
            }
        } else {
            return this.ERROR;
        }
    }

}
